package com.ff.xyh.service.userfront.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.userfront.UserFollower;
import com.ff.xyh.service.userfront.service.UserFollowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户关注表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-25
 */
@RestController
@RequestMapping("/front-user")
public class UserFollowerController {

    @Autowired
    private UserFollowerService userFollowerService;

    @RequestMapping("insert-user-follow")
    public ResultObject insertUserFollow(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "followerId") String followerId ) {
        if ( userFollowerService.getOne(
                new QueryWrapper<UserFollower>()
                        .eq("user_id", id)
                        .eq("follower_id", followerId)) == null){
            if (userFollowerService.save(
                    new UserFollower()
                            .setUserId(id)
                            .setFollowerId(followerId))) {
                return ResultObject.ok().message("成功关注");
            } else {
                return ResultObject.error().message("关注失败");
            }
        } else {
            return ResultObject.error().message("关注失败, 请勿重复关注");
        }
    }

    @RequestMapping("delete-user-follow")
    public ResultObject deleteUserFollow(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "followerId") String followerId ) {
        if (userFollowerService.remove(
                new QueryWrapper<UserFollower>()
                        .eq("user_id", id)
                        .eq("follower_id", followerId))) {
            return ResultObject.ok().message("成功取消关注");
        } else {
            return ResultObject.error().message("取消关注失败");
        }
    }

    /**
     * 删除某一用户所有关注
     * 情景：删除用户或用户注销时使用
     * @param id
     * @return
     */
    @RequestMapping("delete-user-follow-id")
    public ResultObject deleteUserFollowById(
            @RequestParam(name = "id") String id ) {
        if (userFollowerService.remove(
                new QueryWrapper<UserFollower>()
                        .eq("user_id", id))) {
            return ResultObject.ok().message("成功删除该用户所有关注内容");
        } else {
            return ResultObject.error().message(ResultMessage.FAILED_TO_DELETE);
        }
    }


}

